package com.act;

import java.util.ArrayList;

import org.apache.http.Header;
import org.apache.http.message.BasicNameValuePair;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.bean.Answer;
import com.bean.BaseBean;
import com.bean.UserInfo;
import com.common.Constant;
import com.common.TextHttpResponseHandler;
import com.control.UserControl;
import com.xike.kyt.R;
import com.xike.util.MyUtil;
import com.xike.util.StringUtil;

public class CommentAct extends BaseAct implements OnClickListener {

	TextView tv_back, tv_publish;
	EditText edt_content;
	long answerId;
	String answerTitle;

	String strCameraPhotoName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_comment);
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			if (bundle.containsKey("answerId")) {
				answerId = bundle.getLong("answerId");
			}
			if (bundle.containsKey("answerContent")) {
				answerTitle = bundle.getString("answerContent");
			}
		}
		initViews();
	}

	@Override
	protected void initViews() {
		tv_back = (TextView) findViewById(R.id.headTitle_tv_back);
		tv_back.setOnClickListener(this);
		tv_publish = (TextView) findViewById(R.id.comment_tv_publish);

		edt_content = (EditText) findViewById(R.id.comment_edt_content);

		tv_publish.setOnClickListener(this);


	}

	@Override
	protected void updateViews() {
	}

	private void serviceComment() {
		ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("answer_id", String.valueOf(answerId)));
		final String strContent =  MyUtil.getEditTextContent(edt_content);
		params.add(new BasicNameValuePair("content",strContent));
		

		String strUrl = MyUtil.signRequestUrl(Constant.SERVER_URL_ANSWER_COMMENT, params);
		client.get(this, strUrl, new TextHttpResponseHandler(this) {

			@Override
			public void onSuccess(int statusCode, Header[] headers, String responseString) {
				super.onSuccess(statusCode, headers, responseString);
				BaseBean result = gson.fromJson(responseString, BaseBean.class);
				if (result != null && result.isReturnTrue()) {
//					UserControl.setUserInfoChanged(true);
					Intent it_back = new Intent(CommentAct.this, Answer_DetailAct.class);
					Answer answer = new Answer();
					UserInfo user = new UserInfo();
					user.setAvator(UserControl.getLoginUserAvator());
					user.setNick_name(UserControl.getLoginUserName());
					user.setShow_name(UserControl.getLoginUserName());
					user.setId(UserControl.getLoginUserId());
					answer.setUser(user);
					answer.setCreate_time(System.currentTimeMillis());
					answer.setContent(strContent);
					
					it_back.putExtra("newComment", answer);
					setResult(1, it_back);
					finish();
				} else {
					mToast.showToast(CommentAct.this, getResources().getString(R.string.net_error));
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
				mToast.showToast(CommentAct.this, getResources().getString(R.string.net_error));
				super.onFailure(statusCode, headers, responseString, throwable);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.comment_tv_publish:
			if (checkContent()) {
				serviceComment();
			}
			break;
		case R.id.headTitle_tv_back:
			finish();
			break;

		default:
			break;
		}
	}

	private boolean checkContent() {
		String strContent = MyUtil.getEditTextContent(edt_content);
		if (!StringUtil.isNotNull(strContent)) {
			mToast.showToast(this, "评论内容不能为空");
			return false;
		}
		return true;
	}

	protected void onRestart() {
		super.onRestart();
	}

}
